<template lang="html">
  <el-row>
    <el-col :span="24" ref="limitBtnWidth">
      <el-space :size="10" id="actualBtnWidth" class="actualBtnWidth">
        <el-button type="primary" @click="openOpLogDrawer(props.book)" :icon="Document" title="操作日志"
          size="small"></el-button>

        <el-button type="primary" @click="openChapter(props.book)" :icon="Search" size="small" title="目录">目录</el-button>

        <el-button :loading="matchSourceLoading" type="danger" @click="matchSource(props.book)" :icon="RefreshRight"
          size="small" title="同步书源">书源</el-button>

        <el-button :loading="syncChapterLoading" type="danger" @click="syncChapter(props.book)" :icon="RefreshRight"
          size="small" title="同步章节">章节</el-button>

        <el-button :loading="matchSourceLoading" type="danger" @click="matchContent(props.book)" :icon="RefreshRight"
          size="small" title="同步正文">正文</el-button>
      </el-space>
    </el-col>
  </el-row>
  <operation-log-drawer ref="opLogDrawer"></operation-log-drawer>
  <chapter-dialog ref="chapterDialog"></chapter-dialog>
</template>
<script setup lang="js">
import { ref, defineProps, inject } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Search, RefreshRight, Document } from '@element-plus/icons-vue';
import ChapterDialog from '@/views/bookMgr/chapter/components/Dialog.vue';

const props = defineProps({
  book: {
    type: Object,
    required: true
  }
});

const { matchBookSource, syncBookChapter } = inject('bookMgrProvide');
const { createTask, taskCodes } = inject('taskProvide');

const syncChapterLoading = ref(false); // 同步章节加载状态
const opLogDrawer = ref(null); // 操作日志抽屉
const matchSourceLoading = ref(false); // 匹配书源加载状态
const matchContentLoading = ref(false); // 匹配正文加载状态
const chapterDialog = ref(null); // 目录弹窗引用

/**
 * 打开目录
 * @param book 书籍
 */
function openChapter(book) {
  chapterDialog.value.openDialog(book);
}

function syncChapter(book) {
  if (!book.isMatchSource) {
    ElMessage.error("书籍未匹配书源, 无需同步");
    return;
  }
  ElMessageBox.confirm('此操作将同步书籍章节, 是否继续?',
    '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    syncChapterLoading.value = true;
    syncBookChapter(book).then(() => {
      syncChapterLoading.value = false;
    }).catch((error) => {
      syncChapterLoading.value = false;
    });
  }).catch(() => {
    ElMessage.info("已取消同步操作");
  });
}
/**
 * 匹配书源
 * @param book 书籍
 */
function matchSource(book) {
  ElMessageBox.confirm('此操作将同步书书源, 是否继续?',
    '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    matchSourceLoading.value = true;
    matchBookSource(book).then(() => {
      matchSourceLoading.value = false
    }).catch((error) => {
      matchSourceLoading.value = false;
    });
  }).catch(() => {
    matchSourceLoading.value = false;
    ElMessage.info("已取消匹配书源");
  });
}

function matchContent(book) {
  ElMessageBox.confirm('此操作将同步书籍正文, 是否继续?',
    '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    matchContentLoading.value = true;
    createTask({
      taskCode: taskCodes.syncCrawlContent,
      parameters: {
        filter: {
          bookId: book.id,
        }
      },
    }).then(() => {
      matchContentLoading.value = false;
    }).catch((error) => {
      matchContentLoading.value = false;
    });
  }).catch(() => {
    ElMessage.info("已取消匹配正文");
  });
}

function openOpLogDrawer(book) {
  if (opLogDrawer.value) {
    opLogDrawer.value.openDrawer({ sourceId: book.id, title: `${book.title} 操作日志` });
  }
}
</script>
<style lang="">

</style>
